package dobest.ray.com.jiongjiong.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dobest.ray.corelibs.utils.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.bean.ArchivesListBean;

public class ArchivesListAdapter extends BaseAdapter {
    private Context context;
    private List<ArchivesListBean> array = new ArrayList<ArchivesListBean>();

    private int positionSelected = -1;

    public ArchivesListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return array.size();
    }

    @Override
    public ArchivesListBean getItem(int position) {
        // TODO Auto-generated method stub
        return array.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    // type=1 专项视力健康检查
    // type=2 学校视力健康检查
    // type=4 学校视力健康检查
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(
                    R.layout.item_archives_fragment, null);
        }
        View view_divide = view.findViewById(R.id.view_divide);
        if (position == 0) {
            view_divide.setVisibility(View.INVISIBLE);
        } else {
            view_divide.setVisibility(View.VISIBLE);
        }

        TextView tv_dangan_type = ViewHolder.get(view, R.id.tv_dangan_type);
        TextView tv_persent_num = ViewHolder.get(view, R.id.tv_persent_num);
        TextView tv_time = ViewHolder.get(view, R.id.tv_time);

        RelativeLayout rl_main_content = ViewHolder.get(view,
                R.id.rl_main_content);

        LinearLayout ll_content_text = ViewHolder.get(view,
                R.id.ll_content_text);
        ImageView iv_circle = ViewHolder.get(view, R.id.iv_circle);

        tv_time.setText(array.get(position).date);
        tv_persent_num.setText(array.get(position).satur);

        tv_dangan_type.setText(array.get(position).name);

        switch (array.get(position).type) {
            case 1:

                rl_main_content.setBackgroundResource(R.drawable.shape_of_dangan);
                ll_content_text.setBackgroundResource(R.drawable.shape_of_dangan1);

                if (position == positionSelected) {

                    iv_circle.setImageResource(R.mipmap.img_dangan_big);
                } else {
                    iv_circle.setImageResource(R.mipmap.img_dangan_normal);
                }
                break;
            case 2:
            case 4:
                rl_main_content
                        .setBackgroundResource(R.drawable.shape_of_dangan_no_select);
                ll_content_text
                        .setBackgroundResource(R.drawable.shape_of_dangan1_no_select);

                if (position == positionSelected) {

                    iv_circle.setImageResource(R.mipmap.img_dangan_yellow_big);
                } else {
                    iv_circle.setImageResource(R.mipmap.img_dangan_yellow_normal);
                }

                break;

            default:
                break;
        }
        return view;
    }

    public void setData(List<ArchivesListBean> array) {
        this.array.addAll(array);
        notifyDataSetChanged();
    }

    public void setSelected(int position) {
        positionSelected = position;
        notifyDataSetChanged();
    }

    public void refresh(List<ArchivesListBean> listBean) {
        this.array.clear();
        setData(listBean);
    }
}
